package com.mycat.chat.two.ui;

import com.alee.laf.tabbedpane.TabStretchType;
import com.alee.laf.tabbedpane.TabbedPaneStyle;
import com.alee.laf.tabbedpane.WebTabbedPane;
import com.mycat.chat.two.ui.friend.FriendPanel;
import com.mycat.chat.two.ui.group.GroupPanel;
import com.mycat.chat.two.ui.recent.RecentPanel;
import com.mycat.chat.two.util.Constants;
import com.mycat.chat.two.util.PictureUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 主窗口
 *
 * @author roger.
 * @email 190642964@qq.com
 * @create 2017-02-20
 */
public class MainWindow extends JDialog {

    /** 主面板 */
    private JPanel content;
    /** 皮肤按钮 */
    private JLabel skinButton;
    /** 最小化按钮 */
    private JLabel minButton;
    /** 退出按钮 */
    private JLabel exitButton;
    /** 坐标（拖动记录） */
    private Point point = new Point();

    /** 基本信息面板 */
    private JPanel baseInfo;
    /** 好友信息面板 */
    private JPanel userInfo;
    /** 搜索框面板 */
    private JPanel searchInfo;

    /** 标记（左上角） */
    private JLabel productInfo;
    /** 头像 */
    private JLabel picture;
    /** 签名 */
    private JLabel signature;
    /** 昵称 */
    private JLabel nickName;

    /** 搜索框 */
    private JTextField searchText;
    /** 搜索按钮 */
    private JLabel searchButton;
    /** 面板默认背景色 */
    private static Color defaultBgColor = Color.WHITE;

    /** 分类面板（联系人、群组、会话） */
    private WebTabbedPane typeInfo;

    /** 好友列表面板 */
    private FriendPanel friendPanel;
    /** 讨论组、群组面板 */
    private GroupPanel groupPanel;
    /** 最近联系人面板 */
    private RecentPanel recentPanel;

    public MainWindow(){
        super();
        initUI();
        initListener();

        // 设置为屏幕居中
        this.setLocationRelativeTo(null);
        // 启动窗口是在最顶层
        this.setVisible(true);
    }

    /**
     * 初始化页面UI
     */
    public void initUI(){
        // 设置窗口大小
        setSize(300, 649);
        // 一直在最顶层
        setAlwaysOnTop(true);
        // 取消窗口的修饰框
        setUndecorated(true);
        // 用户单击关闭按钮时的程序执行操作，销毁
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // 主面板
        content = new JPanel() {
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(PictureUtil.getPicture("/main/main_back.jpeg").getImage(), 0, 0, null);
                this.setOpaque(false);
            }
        };
        content.setLayout(null);
        // 创建Jpanel的边框
        content.setBorder(Constants.GRAY_BORDER);
        // 将Jpanel设置为居中
        getContentPane().add(content, BorderLayout.CENTER);

        // 最小化按钮
        minButton = new JLabel();
        content.add(minButton);
        minButton.setBounds(233, 0, 31, 20);
        minButton.setIcon(PictureUtil.getPicture("/common/minimize.png"));

        // 关闭按钮
        exitButton = new JLabel();
        content.add(exitButton);
        exitButton.setBounds(261, 0, 39, 20);
        exitButton.setIcon(PictureUtil.getPicture("/common/close.png"));

        // 基本信息面板
        baseInfo = new JPanel();
        content.add(baseInfo);
        baseInfo.setLayout(null);
        baseInfo.setOpaque(false);
        baseInfo.setBounds(0, 0, 300, 118);
        baseInfo.setBorder(Constants.GRAY_BORDER);

        // 左上角标记
        productInfo = new JLabel();
        baseInfo.add(productInfo);
        productInfo.setBounds(8, 0, 45, 20);
        productInfo.setText("MyCat");

        // 头像
        picture = new JLabel();
        baseInfo.add(picture);
        picture.setBounds(7, 33, 66, 66);
        picture.setBorder(Constants.GRAY_BORDER);
        picture.setIcon(new ImageIcon(PictureUtil.getPicture("/login/header_img.png")
                .getImage().getScaledInstance(65, 65, Image.SCALE_DEFAULT)));

        // 昵称
        nickName = new JLabel();
        baseInfo.add(nickName);
        nickName.setFont(Constants.BASIC_FONT2);
        nickName.setText("左佐");
        nickName.setBounds(80, 25, 156, 32);

        // 签名
        signature = new JLabel();
        baseInfo.add(signature);
        signature.setFont(Constants.BASIC_FONT);
        signature.setText("不想当高山上小草的松树不是好松树");
        signature.setToolTipText("不想当高山上小草的松树不是好松树");
        signature.setBounds(80, 59, 156, 32);

        // 皮肤按钮
        skinButton = new JLabel();
        baseInfo.add(skinButton);
        skinButton.setBounds(205, 2, 33, 18);
        skinButton.setIcon(PictureUtil.getPicture("/main/skin.png"));

        // 搜索面板
        searchInfo = new JPanel();
        content.add(searchInfo);
        searchInfo.setLayout(null);
        searchInfo.setOpaque(false);
        searchInfo.setBounds(0, 117, 300, 32);
        searchInfo.setBorder(Constants.GRAY_BORDER);

        searchText = new JTextField();
        searchText.setText(Constants.SPACE + Constants.SEARCH_TXT);
        searchInfo.add(searchText);
        searchText.setOpaque(false);
        searchText.setFocusable(false);
        searchText.setBounds(1, 1, 253, 30);
        searchText.setBorder(BorderFactory.createEmptyBorder());

        searchButton = new JLabel();
        searchInfo.add(searchButton);
        searchButton.setBounds(254, 1, 45, 30);
        searchButton.setOpaque(false);
        searchButton.setBackground(defaultBgColor);
        searchButton.setIcon(PictureUtil.getPicture("/main/search_icon.png"));

        // 好友、联系人、会话面板
        userInfo = new JPanel();
        content.add(userInfo);
        userInfo.setLayout(new BorderLayout());
        userInfo.setOpaque(false);
        userInfo.setBounds(0, 148, 300, 500);
        userInfo.setBackground(defaultBgColor);

        // 类型面板（好友、联系人、会话）
        typeInfo = new WebTabbedPane();
        userInfo.add(typeInfo, BorderLayout.CENTER);
        typeInfo.setOpaque(false);
        typeInfo.setTabbedPaneStyle(TabbedPaneStyle.attached);//不高亮边框
        typeInfo.setTabStretchType(TabStretchType.always);//适应宽度
        typeInfo.setTopBg(new Color(240, 240, 240, 60));
        typeInfo.setBottomBg(new Color(255, 255, 255, 160));
        typeInfo.setSelectedTopBg(new Color(240, 240, 255, 50));
        typeInfo.setSelectedBottomBg(new Color(240, 240, 255, 50));
        typeInfo.setBackground(new Color(255, 255, 255, 200));
        typeInfo.setBorder(Constants.GRAY_BORDER);

        // 好友列表
        friendPanel = new FriendPanel();
        typeInfo.addTab(null, new ImageIcon(PictureUtil.getPicture("/main/tab_boddy.png")
                .getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT)), friendPanel);

        // 群组列表
        groupPanel = new GroupPanel();
        typeInfo.addTab(null, new ImageIcon(PictureUtil.getPicture("/main/tab_group.png")
                .getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT)), groupPanel);

        // 会话列表（最近联系人）
        recentPanel = new RecentPanel();
        typeInfo.addTab(null, new ImageIcon(PictureUtil.getPicture("/main/tab_recent.png")
                .getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT)), recentPanel);

    }

    private void initListener() {
        // 主窗体事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                point.x = e.getX();
                point.y = e.getY();
            }
        });
        // 设置窗口移动监听
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                Point p = getLocation();
                setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
            }
        });
        // 头像区域事件（边框变色）
        picture.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                picture.setBorder(Constants.LIGHT_GRAY_BORDER);
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                picture.setBorder(Constants.ORANGE_BORDER);
            }
        });
        // 换肤按钮事件
        skinButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                skinButton.setIcon(PictureUtil.getPicture("/main/skin.png"));
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                skinButton.setIcon(PictureUtil.getPicture("/main/skin_active.png"));
            }
        });
        // 最小化按钮事件
        minButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                minButton.setIcon(PictureUtil.getPicture("/common/minimize.png"));
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                minButton.setIcon(PictureUtil.getPicture("/common/minimize_active.png"));
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                setVisible(false);
            }
        });
        // 退出按钮事件
        exitButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                exitButton.setIcon(PictureUtil.getPicture("/common/close.png"));
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                exitButton.setIcon(PictureUtil.getPicture("/common/close_active.png"));
            }
            @Override
            public void mouseReleased(MouseEvent e) {
//                tray.remove(icon);
                System.exit(0);
            }
        });
        // 搜索框鼠标事件
        searchText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (Constants.SEARCH_TXT.equals(searchText.getText().trim())) {
                    searchText.setOpaque(true);
                    searchText.setFocusable(true);
                    searchText.requestFocus();
                    searchText.setText("");
                    searchText.updateUI();

                    searchButton.setOpaque(true);
                    searchButton.updateUI();
                }
            }
        });
        // 搜索框焦点事件
        searchText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                searchText.setOpaque(false);
                searchText.setFocusable(false);
                searchText.setText(Constants.SPACE + Constants.SEARCH_TXT);
                searchText.updateUI();

                searchButton.setOpaque(false);
                searchButton.updateUI();
            }
        });
    }

    public static void main(String[] args){
        new MainWindow();
    }
}
